package com.lanou3g.farmdoctor.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.lanou3g.farmdoctor.R;
import com.lanou3g.farmdoctor.activity.QuestionActivity;
import com.lanou3g.farmdoctor.adapter.QandAAdapter;
import com.lanou3g.farmdoctor.base.BaseFragment;

/**
 * 本类由: Risky 创建于: 15/11/24.
 */
public class QandAFragment extends BaseFragment implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
    private Context context;
    private ListView questionList;
    private ImageView searchImg;
    private QandAAdapter qandAAdapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    private Handler handler;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_qanda,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        questionList = (ListView) view.findViewById(R.id.lv_qanda);
        searchImg = (ImageView) view.findViewById(R.id.iv_titlebar_search);
        searchImg.setOnClickListener(this);

        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.srl_qanda);
        swipeRefreshLayout.setOnRefreshListener(this);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                swipeRefreshLayout.setRefreshing(false);
                return false;
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData() {
        qandAAdapter = new QandAAdapter(getActivity());
        questionList.setAdapter(qandAAdapter);
        questionList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //跳转到详情页面
                QuestionActivity.startQuestionActivity(context,qandAAdapter.getItem(position));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_titlebar_search:
                //问题搜索页面
                break;
        }
    }

    @Override
    public void onRefresh() {
        //1秒钟之后回去
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.sendEmptyMessage(1);
            }
        }).start();
    }
}
